<?php
namespace App\Models;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class MyFavorable extends BaseModel
{
    protected $table = "my_favorable";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function fav()
    {
        return $this->belongsTo('App\Models\Favorable', 'favorable_id', 'id');
    }

//    // 获取我的优惠券列表
//    public function myFavorableList(){
//         $where['user_id'] = $request['mid'];
//         $order['id'] = 'desc';
//         $rows = $this->getList($where,'*',$order,10);
//         if ($rows) {
//                $rows = $rows->toArray();
//                $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
//         }
//         return $rows;
//    }


    // 获取优惠券列表
    public function favorableList($request)
    {
        $uid = $request['mid'];
        $where['user_id'] = $uid;
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 10);
        }
        $fav = new Favorable();
        foreach ($rows['data'] as $key => $v) {
            $fav_where['id'] = $v['favorable_id'];
            $fav_info = $fav->getFavorableInfo($fav_where);
            $rows['data'][$key]['title'] = $fav_info['data']['title'];
            $rows['data'][$key]['discount'] = $fav_info['data']['discount'];
            $rows['data'][$key]['type'] = $fav_info['data']['type'];
            $rows['data'][$key]['cash'] = $fav_info['data']['cash'];
        }
        return $rows;
    }

    public function favorableDetail($request)
    {
        $favorable =$this->with('fav')->multiWhere(['id'=>$request['fid']])->first();
        if (!$favorable) {
            return ['state'=>0,'msg'=>'该优惠券不存在'];
        }
        $favorable = $favorable->toArray();
        $favorable['end_time'] = date('Y-m-d H:i:s',$favorable['end_time']);
        $setting = new Setting();
        $settingInfo = $setting->getSetting('system');
        $data['state'] = 1;
        $data['data'] = $favorable;
        $data['hospital_name'] = $settingInfo['hospital_name'];
        $data['qrcode'] = base64_encode(QrCode::format('png')->size(800)->generate($favorable['code_pool']));
        return $data;
    }

}